class Solution
{
public:
    long long countAlternatingSubarrays(vector<int> &nums)
    {
        long long result = 0;
        long long len = 1;
        int n = nums.size();
        for (int i = 1; i < n; ++i)
        {
            if (nums[i] == nums[i - 1])
            {
                result += len * (len + 1) / 2;
                len = 1;
            }
            else
            {
                ++len;
            }
        }
        result += len * (len + 1) / 2;
        return result;
    }
};